package com.ssm.user.controller;

import com.ssm.bean.Result;
import com.ssm.entity.PageResult;
import com.ssm.user.convert.UserConvert;
import com.ssm.user.entity.dto.UserDto;
import com.ssm.user.entity.dto.UserPageDto;
import com.ssm.user.entity.req.UserPageReq;
import com.ssm.user.entity.req.UserReq;
import com.ssm.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @ApiOperation("新增用户接口")
    public Result addUser(@RequestBody UserReq userReq) {
        UserDto userDto = UserConvert.INSTANCE.convertReqToDto(userReq);
        return Result.ok(userService.addUser(userDto));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除用户接口")
    public Result delete(@PathVariable Integer id) {
        return Result.ok(userService.delete(id));
    }
    @GetMapping()
    @ApiOperation("分页查询用户接口")
    public Result<PageResult> userPage(@RequestBody UserPageReq userPageReq) {
        UserPageDto userPageDto = UserConvert.INSTANCE.convertPageReqToPageDto(userPageReq);
        return Result.ok(userService.getUserPage(userPageDto));
    }
}
